package com.mes.web.controller.zc;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mes.entity.cb.Materiel;
import com.mes.entity.hy.Product;
import com.mes.entity.zc.MaterNum;
import com.mes.entity.zc.PnumAndMnum;
import com.mes.entity.zc.ZcBom;
import com.mes.service.zc.CalculateBomService;
import com.mes.utils.DateJsonValueProcessor;
import com.mes.utils.ResponseUtil;
import com.mes.utils.StringUtil;
/**
 * 进行BOM计算，并将结果存入数据库
 * @ClassName: CalculateBomController 
 * @Description:
 * @Author zc
 * @Date 2018年3月27日 下午2:33:33 
 * @version 1.0
 */
@Controller
@RequestMapping("/bom")
public class CalculateBomController {

	@Resource
	private CalculateBomService bomService;
	
	@RequestMapping("/insertBomResult")
	public void insertBomResult(HttpServletRequest request,HttpServletResponse response) throws Exception{
		
		//产品id
		String remartks =request.getParameter("remartks");
		String pnames =request.getParameter("pnames");
		String remartk[] = remartks.split(",");
		String pname[] = pnames.split(",");
		//String remartk1 =request.getParameter("remartk1");
		//添加前清空BOM表
		bomService.deleteBomData();
		for (int x = 0; x < pname.length; x++) {
			System.out.println(remartk[x]);
			//通过产品Id查询产品详情
			List<Product> productList =  bomService.selectProductById(remartk[x]);
			//通过品编查询该产品需要的物料
			String pnum =null;
			PnumAndMnum pam=new PnumAndMnum();
			
			for (Product product : productList) {
				pnum = product.getNum();
				//通过产品编号查询所需要的物料
				
				String pumList = bomService.pumList(pnum);
				System.out.println(pumList);
				if(pumList!=null&&StringUtil.isNotEmpty(pumList)){
					String s[] = pumList.split(",");
					List<Materiel> materielList = null;
					
					for (int i=0;i<s.length;i++) {
						//通过物料和产品ID查询物料数量
						//System.out.println(pnum);
						pam.setPnum(pnum);
						pam.setMnum(s[i]);
						Integer materNum = bomService.selectMaterNumByPnum(pam);
						materielList = bomService.selectMaterielByNum(s[i]);
						
						
						/*for (int j = 0; j < materielList.size(); j++) {
							materielList.get(j).setPname(pname);
							materielList.get(j).setRemartk1(materNum+"");
							
						}*/
						
						//将物料信息传入Bom表中
						for (Materiel materiel : materielList) {
							materiel.setPname(pname[x]);
							materiel.setRemartk1(materNum+"");
							bomService.insertMaterielToBom(materiel);
						}
						
					}
					
					
					
					/*JSONObject jsonObject = new JSONObject();
					JsonConfig jsonConfig = new JsonConfig();
					jsonConfig.registerJsonValueProcessor(java.util.Date.class,new DateJsonValueProcessor("yyyy-MM-dd"));
					JSONArray jsonArray = JSONArray.fromObject(materielList, jsonConfig);
					jsonObject.put("total", null);
					jsonObject.put("rows", jsonArray);
					ResponseUtil.write(response, jsonObject);*/
				}
				
			}
		}
		List<ZcBom> bomList = bomService.bomList();
		//从BOM表中获取数据
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(java.util.Date.class,new DateJsonValueProcessor("yyyy-MM-dd"));
		JSONArray jsonArray = JSONArray.fromObject(bomList, jsonConfig);
		jsonObject.put("total", null);
		jsonObject.put("rows", jsonArray);
		ResponseUtil.write(response, jsonObject);
	}
}
